/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.janalise.control;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.neodatis.odb.ODB;
import org.neodatis.odb.OID;
import br.com.janalise.util.Conexao;

/**
 *
 * @author boskyn9
 */
 abstract class Controller {

    protected Conexao con;
    protected ODB odb;

    protected  void openConexao(){
        con = Conexao.getInstance();
        odb = con.getOdb();
    }

    protected void closeConexao(){
        odb.close();
    }

    protected OID setAndReturnOID(Object obj, Class clazz) throws ClassNotFoundException, NoSuchMethodException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException{

        Class c = Class.forName(clazz.getName(), true, obj.getClass().getClassLoader());
        Method m = c.getMethod("setOid", new Class[]{OID.class});
        m.setAccessible(true);
        OID oid = odb.store(obj);
        m.invoke(obj, oid);
        return oid;
    }

}
